package offer;

import java.util.HashSet;

public class DayFour {
    public static void main(String[] args) {
        int[] params = new int[]{1};
        //findRepeatNumber(params);
        System.out.println(binarySearchLeft(params, 1));
    }
    //数组中重复的数字
    public static int findRepeatNumber(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (!set.add(nums[i])){
                return nums[i];
            }
        }
        return -1;
    }
    //统计一个数字在 排序数组中出现的次数
    public int search(int[] nums, int target) {
       if (nums.length == 0)return 0;
       return 0;
    }

    //二分查找 左侧边界值
    public static int binarySearchLeft(int[] nums,int target){
        if (nums.length == 0) return -1;
        int left = 0,right = nums.length;
        while(left < right){
            int mid = left + (right-left)/2;
            if (nums[mid] == target){
                right = mid;//细节往左区间靠
            }else if(nums[mid] < target){
                left = mid + 1;
            }else if(nums[mid] > target){
                right = mid;
            }
        }
        return left;
    }
    //一个长度为n-1的递增排序数组中的所有数字都是唯一的，并且每个数字都在范围0～n-1之内。在范围0～n-1内的n个数字中有且只有一个数字不在该数组中，请找出这个数字。
    /*
    * 输入: [0,1,2,3,4,5,6,7,9]
       输出: 8
    * */
    public int missingNumber(int[] nums) {
        //第一种方法 hash

        //第二种方法 二分查找  找到数组正常的右侧边界值，返回边界值+1
        int left = 0,right = nums.length-1;
        while(left <= right){
            int mid = left + (right - left)/2;
            if (nums[mid] == mid){
                //收缩左侧边界值
                left = mid+1;
            }else{
                right = mid-1;
            }
        }
        return left+1;
    }
}
